package jzqb.service.impl;

import haha.life.common.BelongToSqlUtils;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.common.Result;
import jzqb.dao.*;
import jzqb.po.*;
import jzqb.service.AdminTechnicalBagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 专题业务层实现类
 */
@Service
public class AdminTechnicalBagServiceImpl implements AdminTechnicalBagService {

    @Autowired
    private AdminZTJDao adminZTJDao;

    @Autowired
    private AdminZTDao adminZTDao;

    @Autowired
    private AdminZTWXDao adminZTWXDao;

    @Autowired
    private AdminUserInfoDao adminUserInfoDao;

    @Override
    public Result insertZTJ(ZTJPo ztjPo) {
        List<ZTJPo> list = adminZTJDao.queryNameAndId(new ZTJPo());
        for (ZTJPo po : list) {
            if(ztjPo.getName().equals(po.getName())){
                return Result.fail("已存在该专题集名称");
            }
        }
        ztjPo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        Boolean result = adminZTJDao.insertZTJ(ztjPo);
        if(result){
            return Result.succ("新建专题集成功");
        }
        return Result.fail("新建专题集失败");
    }

    @Override
    public Boolean updateZTJ(ZTJPo ztjPo) {
        ztjPo.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return adminZTJDao.updateZTJ(ztjPo);
    }

    @Override
    public Boolean deleteZTJ(ZTJPo ztjPo) {
        //删除专题集
        Boolean deleteZTJ = adminZTJDao.deleteZTJ(ztjPo);
        String deleteSql = "专题集ID = " + ztjPo.getId();
        //删除专题
        Boolean deleteZT = adminZTDao.deleteZT(deleteSql);
        //删除专题文献
        Boolean deleteZTWX = adminZTWXDao.deleteZTWX(deleteSql);
        return deleteZTJ && deleteZT && deleteZTWX;
    }

    @Override
    public List<ZTJPo> queryZTJ(AdminQueryBo bo) {
        return adminZTJDao.queryZTJ(bo);
    }

    @Override
    public Long countZTJ(AdminQueryBo bo) {
        return adminZTJDao.count(bo);
    }

    @Override
    public List<ZTPo> queryZT(AdminQueryBo bo) {
        List<ZTPo> ztList = new ArrayList<>();
        String keyWord = bo.getKeyWord();
        //专题集检索名称
        if(!"".equals(keyWord) &&"2".equals(bo.getSearchWay())){
            ZTJPo po = adminZTJDao.queryZTJByName(bo);
            if(po!= null){
                bo.setSearchId(Integer.parseInt(po.getId()));
                bo.setKeyWord(null);
            }else{
                return ztList;
            }
        }
        ztList = adminZTDao.queryZT(bo);
        if(!ztList.isEmpty()){
            String ztjId = "";
            for (ZTPo ztPo : ztList) {
                ztjId += ztPo.getZtjId()+",";
            }
            ztjId = ztjId.substring(0,ztjId.length()-1);
            List<ZTJPo> ztjList = adminZTJDao.queryZTJById(ztjId);
            Map<String, String> map = ztjList.stream().collect(Collectors.toMap(ZTJPo::getId, ZTJPo::getName, (key1, key2) -> key2));
            for (ZTPo ztPo : ztList) {
                ztPo.setZtjName(map.get(ztPo.getZtjId()));
            }
        }
        return ztList;
    }

    @Override
    public Long countZT(AdminQueryBo bo) {
        return adminZTDao.countZT(bo);
    }

    @Override
    public List<ZTJPo> queryZTJList(String ztjName) {
        ZTJPo ztjPo = new ZTJPo();
        ztjPo.setName(ztjName);
        return adminZTJDao.queryNameAndId(ztjPo);
    }

    @Override
    public Result insertZT(ZTPo ztPo) {
        List<ZTPo> list = adminZTDao.queryNameAndId(ztPo);
        Boolean isExistZTName = false;
        Boolean isChangeZTSortId = true;
        String changeId = "";
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if(ztPo.getBeforeSortId() == (ztPo.getSortId())){
            isChangeZTSortId = false;
        }
        for (ZTPo po : list) {
            if(po.getName().equals(ztPo.getName()) && !po.getId().equals(ztPo.getId())){
                isExistZTName = true;
            }
            if(isChangeZTSortId){
                if(ztPo.getSortId() == (po.getSortId())){
                    changeId = po.getId();
                }
            }
        }
        if(isExistZTName){
            return Result.fail("该专题集下已存在该专题，请修改专题名称！");
        }
        if(!isExistZTName && isChangeZTSortId){
            ZTPo po = new ZTPo();
            po.setId(changeId);
            po.setSortId(ztPo.getBeforeSortId());
            po.setUpdateTime(format);
            Boolean zt = adminZTDao.updateZT(po);
        }
        if(ztPo.getSortId() == null && ztPo.getBeforeSortId() != null){
            ztPo.setSortId(ztPo.getBeforeSortId());
        }
        ztPo.setCreateTime(format);
        ztPo.setUpdateTime(format);
        Boolean result = adminZTDao.insertZT(ztPo);
        if(result){
            return Result.succ("新建专题成功");
        }
        return Result.fail("新建专题失败");
    }

    @Override
    public Result updateZT(ZTPo ztPo) {
        List<ZTPo> list = adminZTDao.queryNameAndId(ztPo);
        Boolean isExistZTName = false;
        Boolean isChangeZTSortId = true;
        String changeId = "";
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if(ztPo.getBeforeSortId() == (ztPo.getSortId())){
            isChangeZTSortId = false;
        }
        for (ZTPo po : list) {
            if(ztPo.getName().equals(po.getName()) && !ztPo.getId().equals(po.getId())){
                isExistZTName = true;
            }
            if(isChangeZTSortId){
                if(ztPo.getSortId() == (po.getSortId())){
                    changeId = po.getId();
                }
            }
        }
        if(isExistZTName){
            return Result.fail("该专题集下已存在该专题，请修改专题名称！");
        }
        if(!isExistZTName && isChangeZTSortId){
            ZTPo po = new ZTPo();
            po.setId(changeId);
            po.setSortId(ztPo.getBeforeSortId());
            po.setUpdateTime(format);
            Boolean zt = adminZTDao.updateZT(po);
        }
        if(ztPo.getSortId() == null && ztPo.getBeforeSortId() != null){
            ztPo.setSortId(ztPo.getBeforeSortId());
        }
        ztPo.setUpdateTime(format);
        Boolean result = adminZTDao.updateZT(ztPo);
        if(result){
            return Result.succ("修改专题成功");
        }
        return Result.fail("修改专题失败");
    }

    @Override
    public Boolean deleteZT(ZTPo ztPo) {
        //删除专题
        String deleteSql = "ID = " + ztPo.getId();
        Boolean deleteZT = adminZTDao.deleteZT(deleteSql);
        //删除专题文献
        deleteSql = "专题ID = " + ztPo.getId();
        Boolean deleteZTWX = adminZTWXDao.deleteZTWX(deleteSql);
        return deleteZT;
    }

    @Override
    public List<ZTWXPo> queryZTWX(AdminQueryBo bo) {
        String[] standardType = bo.getStandardType();
        String type ="";
        List<ZTWXPo>  list = new ArrayList<>();
        //处理勾选领域
        if(standardType.length == 8){
            type = null;
        }else{
            type = BelongToSqlUtils.getBelongToSql(standardType, "领域");
        }
        bo.setType(type);
        list = adminZTWXDao.queryZTWX(bo);
        if (list != null && !list.isEmpty()) {
            String ztjId = "";
            String recorderId = "";
            String ztId = "";
            for (ZTWXPo po : list) {
                //处理专题集名称
                ztjId += po.getZtjId() + ",";
                //处理专题名称
                ztId += po.getZtId() + ",";
                //处理录入人姓名
                recorderId += po.getRecorder() + ",";
            }
            ztjId = ztjId.substring(0,ztjId.length()-1);
            ztId = ztId.substring(0,ztId.length()-1);
            recorderId = recorderId.substring(0,recorderId.length()-1);
            List<ZTJPo> ztjList = adminZTJDao.queryZTJById(ztjId);
            List<AdminUserPo> userNameList = adminUserInfoDao.getUserNameById(recorderId);
            //对查询ID做处理
            String[] ztArr= ztId.split(",");
            String ztSql = BelongToSqlUtils.getBelongToSql(ztArr, "ID");
            AdminQueryBo queryBo = new AdminQueryBo();
            queryBo.setType(ztSql);
            List<ZTPo> ztList = adminZTDao.queryZTList(queryBo);
            //查询专题，专题集，录入人的ID与名字并放入map
            Map<String, String> ztjMap = ztjList.stream().collect(Collectors.toMap(ZTJPo::getId, ZTJPo::getName, (key1, key2) -> key2));
            Map<String, String> ztMap = ztList.stream().collect(Collectors.toMap(ZTPo::getId, ZTPo::getName, (key1, key2) -> key2));
            Map<String, String> userMap = userNameList.stream().collect(Collectors.toMap(AdminUserPo::getId, AdminUserPo::getName, (key1, key2) -> key2));
            for (ZTWXPo ztwxPo : list) {
                ztwxPo.setRecorderName(userMap.get(ztwxPo.getRecorder()));
                ztwxPo.setZtjName(ztjMap.get(ztwxPo.getZtjId()));
                ztwxPo.setZtName(ztMap.get(ztwxPo.getZtId()));
            }
        }
        return list;
    }

    @Override
    public Long countZTWX(AdminQueryBo bo) {
        return adminZTWXDao.countZTWX(bo);
    }

    @Override
    public Boolean insertZTWX(ZTWXPo po) {
        List<ZTWXPo> list = adminZTWXDao.queryNameAndId(po);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if (list != null && !list.isEmpty()) {
            for (ZTWXPo po1 : list) {
                if((po.getZtSortId()==po1.getZtSortId()) && (po.getId()!= po1.getId())){
                    po1.setZtSortId(po.getBeforeSortId());
                    po1.setRecordUpdateDate(format);
                    adminZTWXDao.updateZTWX(po1);
                    break;
                }
            }
        }
        po.setRecordAddDate(format);
        po.setRecordUpdateDate(format);
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        if(po.getZtSortId() == null && po.getBeforeSortId() != null){
            po.setZtSortId(po.getBeforeSortId());
        }
        return adminZTWXDao.insertZTWX(po);
    }

    @Override
    public Boolean updateZTWX(ZTWXPo po) {
        List<ZTWXPo> list = adminZTWXDao.queryNameAndId(po);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        if (list != null && !list.isEmpty()) {
            for (ZTWXPo po1 : list) {
                if (po.getZtSortId() == po1.getZtSortId() && po.getId() != po1.getId()) {
                    po1.setZtSortId(po.getBeforeSortId());
                    po1.setRecordUpdateDate(format);
                    adminZTWXDao.updateZTWX(po1);
                    break;
                }
            }
        }
        po.setRecordAddDate(format);
        po.setRecordUpdateDate(format);
        String year = FileuploadUtils.getYear(po.getPublishDate());
        po.setYear(year);
        if(po.getZtSortId() == null && po.getBeforeSortId() != null){
            po.setZtSortId(po.getBeforeSortId());
        }
        return adminZTWXDao.updateZTWX(po);
    }


    @Override
    public Boolean deleteZTWX(ZTWXPo ztwxPo) {
        String deleteSql = "SYS_FLD_SYSID = " + ztwxPo.getSysID();
        return adminZTWXDao.deleteZTWX(deleteSql);
    }

    @Override
    public List<ZTPo> queryZTList(AdminQueryBo bo) {
        return adminZTDao.queryZTList(bo);
    }

    @Override
    public List<ZTWXPo> getMaxZTWXId(AdminQueryBo bo) {
        return adminZTWXDao.getMaxZTWXId(bo);
    }


}
